Skip to content

Conversation

mordante
Copy link
Member

@mordante mordante commented Aug 3, 2024

This patch is the start of a series to improve the speed of std::format, std::format_to, std::format_to_n, and std::formatted_size.

This is mostly achieved by changing the __output_buffer class. This new __output_buffer class also makes it easier to implement buffering for P3107R5 "Permit an efficient implementation of std::print"

@mordante mordante requested a review from a team as a code owner August 3, 2024 08:36
@llvmbot llvmbot added the libc++ libc++ C++ Standard Library. Not GNU libstdc++. Not libc++abi. label Aug 3, 2024
@llvmbot
Copy link
Member

llvmbot commented Aug 3, 2024

@llvm/pr-subscribers-libcxx

Author: Mark de Wever (mordante)

Changes

This patch is the start of a series to improve the speed of std::format, std::format_to, std::format_to_n, and std::formatted_size.

This is mostly achieved by changing the __output_buffer class. This new __output_buffer class also makes it easier to implement buffering for P3107R5 "Permit an efficient implementation of std::print"


Patch is 22.06 KiB, truncated to 20.00 KiB below, full version: https://github.com/llvm/llvm-project/pull/101803.diff

4 Files Affected:

  • (modified) libcxx/test/benchmarks/CMakeLists.txt (+3)
  • (added) libcxx/test/benchmarks/format/write_double_comparison.bench.cpp (+106)
  • (added) libcxx/test/benchmarks/format/write_int_comparison.bench.cpp (+106)
  • (added) libcxx/test/benchmarks/format/write_string_comparison.bench.cpp (+220)
diff --git a/libcxx/test/benchmarks/CMakeLists.txt b/libcxx/test/benchmarks/CMakeLists.txt
index d61367a367738..6849305257ab0 100644
--- a/libcxx/test/benchmarks/CMakeLists.txt
+++ b/libcxx/test/benchmarks/CMakeLists.txt
@@ -146,6 +146,9 @@ set(BENCHMARK_TESTS
     deque_iterator.bench.cpp
     exception_ptr.bench.cpp
     filesystem.bench.cpp
+    format/write_double_comparison.bench.cpp
+    format/write_int_comparison.bench.cpp
+    format/write_string_comparison.bench.cpp
     format_to_n.bench.cpp
     format_to.bench.cpp
     format.bench.cpp
diff --git a/libcxx/test/benchmarks/format/write_double_comparison.bench.cpp b/libcxx/test/benchmarks/format/write_double_comparison.bench.cpp
new file mode 100644
index 0000000000000..29851d02fa600
--- /dev/null
+++ b/libcxx/test/benchmarks/format/write_double_comparison.bench.cpp
@@ -0,0 +1,106 @@
+//===----------------------------------------------------------------------===//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#include <array>
+#include <charconv>
+#include <cstdio>
+#include <format>
+#include <iterator>
+#include <list>
+#include <random>
+#include <vector>
+
+#include "benchmark/benchmark.h"
+
+std::array data = [] {
+  std::uniform_real_distribution<double> distribution;
+  std::mt19937 generator;
+  std::array<double, 1000> result;
+  std::generate_n(result.begin(), result.size(), [&] { return distribution(generator); });
+  return result;
+}();
+
+static void BM_sprintf(benchmark::State& state) {
+  std::array<char, 100> output;
+  while (state.KeepRunningBatch(data.size()))
+    for (auto value : data)
+      benchmark::DoNotOptimize(sprintf(output.data(), "%f", value));
+}
+
+static void BM_to_string(benchmark::State& state) {
+  while (state.KeepRunningBatch(data.size()))
+    for (auto value : data)
+      benchmark::DoNotOptimize(std::to_string(value));
+}
+
+static void BM_to_chars(benchmark::State& state) {
+  std::array<char, 100> output;
+
+  while (state.KeepRunningBatch(data.size()))
+    for (auto value : data)
+      benchmark::DoNotOptimize(std::to_chars(output.data(), output.data() + output.size(), value));
+}
+
+static void BM_to_chars_as_string(benchmark::State& state) {
+  std::array<char, 100> output;
+
+  while (state.KeepRunningBatch(data.size()))
+    for (auto value : data) {
+      char* end = std::to_chars(output.data(), output.data() + output.size(), value).ptr;
+      std::string s{output.data(), end};
+      benchmark::DoNotOptimize(s);
+    }
+}
+
+static void BM_format(benchmark::State& state) {
+  while (state.KeepRunningBatch(data.size()))
+    for (auto value : data)
+      benchmark::DoNotOptimize(std::format("{}", value));
+}
+
+template <class C>
+static void BM_format_to_back_inserter(benchmark::State& state) {
+  while (state.KeepRunningBatch(data.size()))
+    for (auto value : data) {
+      C c;
+      std::format_to(std::back_inserter(c), "{}", value);
+      benchmark::DoNotOptimize(c);
+    }
+}
+
+template <class F>
+static void BM_format_to_iterator(benchmark::State& state, F&& f) {
+  auto output = f();
+  while (state.KeepRunningBatch(data.size()))
+    for (auto value : data)
+      benchmark::DoNotOptimize(std::format_to(std::begin(output), "{}", value));
+}
+
+BENCHMARK(BM_sprintf);
+BENCHMARK(BM_to_string);
+BENCHMARK(BM_to_chars);
+BENCHMARK(BM_to_chars_as_string);
+BENCHMARK(BM_format);
+BENCHMARK_TEMPLATE(BM_format_to_back_inserter, std::string);
+BENCHMARK_TEMPLATE(BM_format_to_back_inserter, std::vector<char>);
+BENCHMARK_TEMPLATE(BM_format_to_back_inserter, std::list<char>);
+BENCHMARK_CAPTURE(BM_format_to_iterator, <std::array>, ([] {
+                    std::array<char, 100> a;
+                    return a;
+                  }));
+BENCHMARK_CAPTURE(BM_format_to_iterator, <std::string>, ([] {
+                    std::string s;
+                    s.resize(100);
+                    return s;
+                  }));
+BENCHMARK_CAPTURE(BM_format_to_iterator, <std::vector>, ([] {
+                    std::vector<char> v;
+                    v.resize(100);
+                    return v;
+                  }));
+
+BENCHMARK_MAIN();
diff --git a/libcxx/test/benchmarks/format/write_int_comparison.bench.cpp b/libcxx/test/benchmarks/format/write_int_comparison.bench.cpp
new file mode 100644
index 0000000000000..6390558fbc6e7
--- /dev/null
+++ b/libcxx/test/benchmarks/format/write_int_comparison.bench.cpp
@@ -0,0 +1,106 @@
+//===----------------------------------------------------------------------===//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#include <array>
+#include <charconv>
+#include <cstdio>
+#include <format>
+#include <iterator>
+#include <list>
+#include <random>
+#include <vector>
+
+#include "benchmark/benchmark.h"
+
+std::array data = [] {
+  std::uniform_int_distribution<int> distribution{std::numeric_limits<int>::min(), std::numeric_limits<int>::max()};
+  std::mt19937 generator;
+  std::array<int, 1000> result;
+  std::generate_n(result.begin(), result.size(), [&] { return distribution(generator); });
+  return result;
+}();
+
+static void BM_sprintf(benchmark::State& state) {
+  std::array<char, 100> output;
+  while (state.KeepRunningBatch(data.size()))
+    for (auto value : data)
+      benchmark::DoNotOptimize(sprintf(output.data(), "%d", value));
+}
+
+static void BM_to_string(benchmark::State& state) {
+  while (state.KeepRunningBatch(data.size()))
+    for (auto value : data)
+      benchmark::DoNotOptimize(std::to_string(value));
+}
+
+static void BM_to_chars(benchmark::State& state) {
+  std::array<char, 100> output;
+
+  while (state.KeepRunningBatch(data.size()))
+    for (auto value : data)
+      benchmark::DoNotOptimize(std::to_chars(output.data(), output.data() + output.size(), value));
+}
+
+static void BM_to_chars_as_string(benchmark::State& state) {
+  std::array<char, 100> output;
+
+  while (state.KeepRunningBatch(data.size()))
+    for (auto value : data) {
+      char* end = std::to_chars(output.data(), output.data() + output.size(), value).ptr;
+      std::string s{output.data(), end};
+      benchmark::DoNotOptimize(s);
+    }
+}
+
+static void BM_format(benchmark::State& state) {
+  while (state.KeepRunningBatch(data.size()))
+    for (auto value : data)
+      benchmark::DoNotOptimize(std::format("{}", value));
+}
+
+template <class C>
+static void BM_format_to_back_inserter(benchmark::State& state) {
+  while (state.KeepRunningBatch(data.size()))
+    for (auto value : data) {
+      C c;
+      std::format_to(std::back_inserter(c), "{}", value);
+      benchmark::DoNotOptimize(c);
+    }
+}
+
+template <class F>
+static void BM_format_to_iterator(benchmark::State& state, F&& f) {
+  auto output = f();
+  while (state.KeepRunningBatch(data.size()))
+    for (auto value : data)
+      benchmark::DoNotOptimize(std::format_to(std::begin(output), "{}", value));
+}
+
+BENCHMARK(BM_sprintf);
+BENCHMARK(BM_to_string);
+BENCHMARK(BM_to_chars);
+BENCHMARK(BM_to_chars_as_string);
+BENCHMARK(BM_format);
+BENCHMARK_TEMPLATE(BM_format_to_back_inserter, std::string);
+BENCHMARK_TEMPLATE(BM_format_to_back_inserter, std::vector<char>);
+BENCHMARK_TEMPLATE(BM_format_to_back_inserter, std::list<char>);
+BENCHMARK_CAPTURE(BM_format_to_iterator, <std::array>, ([] {
+                    std::array<char, 100> a;
+                    return a;
+                  }));
+BENCHMARK_CAPTURE(BM_format_to_iterator, <std::string>, ([] {
+                    std::string s;
+                    s.resize(100);
+                    return s;
+                  }));
+BENCHMARK_CAPTURE(BM_format_to_iterator, <std::vector>, ([] {
+                    std::vector<char> v;
+                    v.resize(100);
+                    return v;
+                  }));
+
+BENCHMARK_MAIN();
diff --git a/libcxx/test/benchmarks/format/write_string_comparison.bench.cpp b/libcxx/test/benchmarks/format/write_string_comparison.bench.cpp
new file mode 100644
index 0000000000000..e32bee08a7ca8
--- /dev/null
+++ b/libcxx/test/benchmarks/format/write_string_comparison.bench.cpp
@@ -0,0 +1,220 @@
+//===----------------------------------------------------------------------===//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#include <array>
+#include <concepts>
+#include <cstdio>
+#include <deque>
+#include <format>
+#include <iterator>
+#include <list>
+#include <string>
+#include <string_view>
+#include <vector>
+
+#include "benchmark/benchmark.h"
+#include "test_macros.h"
+
+const char* c_string_6_characters  = "abcdef";
+const char* c_string_60_characters = "abcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdef";
+const char* c_string_6000_characters =
+    "abcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdef"
+    "abcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdef"
+    "abcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdef"
+    "abcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdef"
+    "abcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdef"
+    "abcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdef"
+    "abcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdef"
+    "abcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdef"
+    "abcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdef"
+    "abcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdef"
+    "abcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdef"
+    "abcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdef"
+    "abcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdef"
+    "abcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdef"
+    "abcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdef"
+    "abcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdef"
+    "abcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdef"
+    "abcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdef"
+    "abcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdef"
+    "abcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdef"
+    "abcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdef"
+    "abcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdef"
+    "abcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdef"
+    "abcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdef"
+    "abcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdef"
+    "abcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdef"
+    "abcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdef"
+    "abcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdef"
+    "abcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdef"
+    "abcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdef"
+    "abcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdef"
+    "abcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdef"
+    "abcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdef"
+    "abcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdef"
+    "abcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdef"
+    "abcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdef"
+    "abcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdef"
+    "abcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdef"
+    "abcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdef"
+    "abcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdef"
+    "abcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdef"
+    "abcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdef"
+    "abcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdef"
+    "abcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdef"
+    "abcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdef"
+    "abcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdef"
+    "abcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdef"
+    "abcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdef"
+    "abcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdef"
+    "abcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdef"
+    "abcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdef"
+    "abcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdef"
+    "abcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdef"
+    "abcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdef"
+    "abcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdef"
+    "abcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdef"
+    "abcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdef"
+    "abcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdef"
+    "abcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdef"
+    "abcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdef"
+    "abcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdef"
+    "abcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdef"
+    "abcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdef"
+    "abcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdef"
+    "abcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdef"
+    "abcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdef"
+    "abcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdef"
+    "abcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdef"
+    "abcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdef"
+    "abcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdef"
+    "abcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdef"
+    "abcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdef"
+    "abcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdef"
+    "abcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdef"
+    "abcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdef"
+    "abcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdef"
+    "abcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdef"
+    "abcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdef"
+    "abcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdef"
+    "abcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdef"
+    "abcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdef"
+    "abcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdef"
+    "abcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdef"
+    "abcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdef"
+    "abcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdef"
+    "abcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdef"
+    "abcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdef"
+    "abcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdef"
+    "abcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdef"
+    "abcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdef"
+    "abcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdef"
+    "abcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdef"
+    "abcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdef"
+    "abcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdef"
+    "abcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdef"
+    "abcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdef"
+    "abcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdef"
+    "abcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdef"
+    "abcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdef"
+    "abcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdef"; // 100 lines
+
+std::string string_6_characters    = c_string_6_characters;
+std::string string_60_characters   = c_string_60_characters;
+std::string string_6000_characters = c_string_6000_characters;
+
+std::string_view string_view_6_characters    = c_string_6_characters;
+std::string_view string_view_60_characters   = c_string_60_characters;
+std::string_view string_view_6000_characters = c_string_6000_characters;
+
+static void BM_sprintf(benchmark::State& state, const char* value) {
+  std::array<char, 10'000> output;
+  for (auto _ : state)
+    benchmark::DoNotOptimize(sprintf(output.data(), "%s", value));
+}
+
+template <class T>
+static void BM_format(benchmark::State& state, const T& value) {
+  for (auto _ : state)
+    benchmark::DoNotOptimize(std::format("{}", value));
+}
+
+template <class C, class T>
+static void BM_format_to_back_inserter(benchmark::State& state, const T& value) {
+  for (auto _ : state) {
+    C c;
+    std::format_to(std::back_inserter(c), "{}", value);
+    benchmark::DoNotOptimize(c);
+  }
+}
+
+template <class T, class F>
+static void BM_format_to_iterator(benchmark::State& state, const T& value, F&& f) {
+  auto output = f();
+  for (auto _ : state) {
+    benchmark::DoNotOptimize(std::format_to(std::begin(output), "{}", value));
+  }
+}
+
+#define FORMAT_BENCHMARKS(name, variable)                                                                              \
+  BENCHMARK_CAPTURE(BM_format, name, variable);                                                                        \
+                                                                                                                       \
+  BENCHMARK_TEMPLATE1_CAPTURE(BM_format_to_back_inserter, std::string, name, variable);                                \
+  BENCHMARK_TEMPLATE1_CAPTURE(BM_format_to_back_inserter, std::vector<char>, name, variable);                          \
+  BENCHMARK_TEMPLATE1_CAPTURE(BM_format_to_back_inserter, std::deque<char>, name, variable);                           \
+  BENCHMARK_TEMPLATE1_CAPTURE(BM_format_to_back_inserter, std::list<char>, name, variable);                            \
+                                                                                                                       \
+  BENCHMARK_CAPTURE(BM_format_to_iterator, <std::array> name, variable, ([] {                                          \
+                      std::array<char, 10'000> a;                                                                      \
+                      return a;                                                                                        \
+                    }));                                                                                               \
+  BENCHMARK_CAPTURE(BM_format_to_iterator, <std::string> name, variable, ([] {                                         \
+                      std::string s;                                                                                   \
+                      s.resize(10'000);                                                                                \
+                      return s;                                                                                        \
+                    }));                                                                                               \
+  BENCHMARK_CAPTURE(BM_format_to_iterator, <std::vector> name, variable, ([] {                                         \
+                      std::vector<char> v;                                                                             \
+                      v.resize(10'000);                                                                                \
+                      return v;                                                                                        \
+                    }));                                                                                               \
+  BENCHMARK_CAPTURE(BM_format_to_iterator, <std::deque> name, variable, ([] {                                          \
+                      std::deque<char> d;                                                                              \
+                      d.resize(10'000);                                   ...
[truncated]

Copy link
Member

@ldionne ldionne left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM with a few changes.

@mordante mordante force-pushed the users/mordante/format_performance__benchmarks branch from d4376a4 to 7d63915 Compare September 17, 2024 15:21
@mordante mordante force-pushed the users/mordante/format_performance__benchmarks branch from 7d63915 to 8db9ff2 Compare October 6, 2024 11:37
This patch is the start of a series to improve the speed of std::format,
std::format_to, std::format_to_n, and std::formatted_size.

This is mostly achieved by changing the __output_buffer class. This new
__output_buffer class also makes it easier to implement buffering for
P3107R5 "Permit an efficient implementation of std::print"
@mordante mordante force-pushed the users/mordante/format_performance__benchmarks branch from 8db9ff2 to 161b481 Compare October 6, 2024 14:11
@mordante mordante merged commit d54b1cf into main Oct 6, 2024
64 checks passed
@mordante mordante deleted the users/mordante/format_performance__benchmarks branch October 6, 2024 16:19
Kyvangka1610 added a commit to Kyvangka1610/llvm-project that referenced this pull request Oct 6, 2024
* commit 'FETCH_HEAD':
  [X86] combineAndLoadToBZHI - don't do an return early return if we fail to match a load
  [X86] replace-load-and-with-bzhi.ll - add commuted test cases to show failure to fold
  [X86] replace-load-and-with-bzhi.ll - cleanup check-prefixes to use X86/X64 for 32/64-bit targets
  [ExecutionEngine] Avoid repeated hash lookups (NFC) (llvm#111275)
  [ByteCode] Avoid repeated hash lookups (NFC) (llvm#111273)
  [StaticAnalyzer] Avoid repeated hash lookups (NFC) (llvm#111272)
  [CodeGen] Avoid repeated hash lookups (NFC) (llvm#111274)
  [RISCV] Simplify fixed-vector-fp.ll run lines. NFC
  [libc++][format][1/3] Adds more benchmarks. (llvm#101803)
  [X86] combineOrXorWithSETCC - avoid duplicate SDLoc/operands code. NFC.
  [X86] convertIntLogicToFPLogic - avoid duplicate SDLoc/operands code. NFC.
  [libc] Clean up some include in `libc`. (llvm#110980)
  [X86] combineBitOpWithPACK - avoid duplicate SDLoc/operands code. NFC.
  [X86] combineBitOpWithMOVMSK - avoid duplicate SDLoc/operands code. NFC.
  [X86] combineBitOpWithShift - avoid duplicate SDLoc/operands code. NFC.
  [x86] combineMul - use computeKnownBits directly to find MUL_IMM constant splat.
  [X86] combineSubABS - avoid duplicate SDLoc. NFC.
  [ValueTypes][RISCV] Add v1bf16 type (llvm#111112)
  [VPlan] Add additional FOR hoisting test.
  [clang-tidy] Create bugprone-bitwise-pointer-cast check (llvm#108083)
  [InstCombine] Canonicalize more geps with constant gep bases and constant offsets. (llvm#110033)
  [LV] Honor uniform-after-vectorization in setVectorizedCallDecision.
  [ELF] Pass Ctx & to Arch/
  [ELF] Pass Ctx & to Arch/
  [libc++] Fix a typo (llvm#111239)
  [X86] For minsize memset/memcpy, use byte or double-word accesses (llvm#87003)
  [RISCV] Unify RVBShift_ri and RVBShiftW_ri with Shift_ri and ShiftW_ri. NFC (llvm#111263)
  Revert "Reapply "[AMDGPU][GlobalISel] Fix load/store of pointer vectors, buffer.*.pN (llvm#110714)" (llvm#111059)"
  [libc] Add missing include to __support/StringUtil/tables/stdc_errors.h. (llvm#111271)
  [libc] remove errno.h includes (llvm#110934)
  [NFC][rtsan] Update docs to include [[clang::blocking]] (llvm#111249)
  [RISCV] Give ZEXT_H_RV32 and ZEXT_H_RV64 R-type format to match PACK. NFC
  [mlir][SPIRV] Fix build (2) (llvm#111265)
  [mlir][SPIRV] Fix build error (llvm#111264)
  [mlir][NFC] Mark type converter in `populate...` functions as `const` (llvm#111250)
  [Basic] Avoid repeated hash lookups (NFC) (llvm#111228)
  [RISCV] Use THShift_ri class instead of RVBShift_ri for TH_TST instruction. NFC
  [VPlan] Only generate first lane for VPPredInstPHI if no others used.
  [ELF] Don't call getPPC64TargetInfo outside Driver. NFC
  [GISel] Don't preserve NSW flag when converting G_MUL of INT_MIN to G_SHL. (llvm#111230)
  [APInt] Slightly simplify APInt::ashrSlowCase. NFC (llvm#111220)
  [Sema] Avoid repeated hash lookups (NFC) (llvm#111227)
  [Affine] Avoid repeated hash lookups (NFC) (llvm#111226)
  [Driver] Avoid repeated hash lookups (NFC) (llvm#111225)
  [clang][test] Remove a broken bytecode test
  [ELF] Pass Ctx &
  [ELF] Pass Ctx & to Relocations

Signed-off-by: kyvangka1610 <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
libc++ libc++ C++ Standard Library. Not GNU libstdc++. Not libc++abi.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants